// 静态属性，样式属性，剥离数据 多种屏幕尺寸都事先部署好

const staticOption = {
  legend: {
    show:false,
    orient: 'vertical',
    x: 'right',
    y: 'center',
    left: '75%',
    data: ['户籍生', '政策生', '新居民'],
    textStyle: {
      color: '#fff',
      fontSize: '16'
    }
  },
  tooltip: {
    name: '数量',
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  color: ['#1890ff', '#00e2fe', '#ffea7f', '#0069df', '#7574ee', '#fb6c84', '#ee7eb2', '#e5beeb', '#7574ee',
    '#0069df'
  ],
  series: [
    {
      name: '学生人数',
      type: 'pie',
      radius: ['30%', '55%'],
      center: ['50%', '50%'],
      avoidLabelOverlap: true,
      roseType: 'radius',
      label: {
        show: true,
        fontSize: 16
      },
      data: []
    }
  ]
}

const staticOptionHuge = {
  tooltip: {
    name: '数量',
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  color: ['#30a0d1', '#2ac2e2', '#9ee2bd', '#ffdd89', '#ffa286', '#fb6c84', '#ee7eb2', '#e5beeb', '#7574ee',
    '#0069df'
  ],
  series: [
    {
      name: '生源占比',
      type: 'pie',
      radius: ['40%', '65%'],
      avoidLabelOverlap: true,
      roseType: 'radius',
      label: {
        show: true,
        fontSize: 16
      },
      data: []
    }
  ]
}

const staticOptionMid = {
  tooltip: {
    name: '数量',
    trigger: 'item',
    formatter: '{a} <br/>{b}: {c} ({d}%)'
  },
  color: ['#30a0d1', '#2ac2e2', '#9ee2bd', '#ffdd89', '#ffa286', '#fb6c84', '#ee7eb2', '#e5beeb', '#7574ee',
    '#0069df'
  ],
  series: [
    {
      name: '生源占比',
      type: 'pie',
      radius: ['40%', '65%'],
      avoidLabelOverlap: true,
      roseType: 'radius',
      label: {
        show: true,
        fontSize: 16
      },
      data: []
    }
  ]
}

// 备选颜色，有需求则进行循环
// const colors = ['#00c0a7', '#1e8fff']
// let colorIndex = 0

// 获取设备宽度
const screen = window.screen.width

class ChartFactory {
  constructor (additionalOptions) {
    // 根据屏幕宽度获取不同配置的echarts配置
    this.option = staticOption
    if (screen <= 1600) {
      this.option = staticOptionMid
    }
    if (screen >= 3656) {
      this.option = staticOptionHuge
    }
    // 下方复制属于浅拷贝，指向同个内存，针对可能有更改的需求，要进行一次清理
    this.clear()
    // // 装配数据 必须使用箭头函数
    // colorIndex = 0
    additionalOptions.seriesData.forEach((p1, p2) => {
      this.option.series[0].data.push(p1)
    })
    this.option.tooltip.formatter = (params) => {
      return `${params.name} <br />
              ${params.seriesName}:${additionalOptions.seriesData[params.dataIndex].ratio}(${(params.value * 100).toFixed(2)}%)`
    }
  }

  // 清理方法，要将部署的新数据全部置空
  clear () {
    this.option.series[0].data = []
  }

  getOption () {
    return this.option
  }
}

export { ChartFactory }
